package com.itita.weibo.ui;


 
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.itita.weibo.manager.IWeiboActivity;
import com.itita.weibo.manager.MainService;
import com.itita.weibo.model.ImageFaceAdapter;
import com.itita.weibo.model.Task;
import com.itita.weibo.util.WeiboUtil;


public final class CommentActivity extends Activity implements IWeiboActivity, TextWatcher, OnClickListener{
	
	
	private View title;
	
	private Button back,send;
	
	private TextView titleTv;
	
	private EditText editText;
	
	private TextView tvCmtLabel;
	
	private CheckBox rb_forward;
	
	//添加表情
	private ImageButton btnFaces ;

    //插入话题
	private ImageButton ibInsertTopic;
    
    //插入用户名
	private ImageButton ibInsertAt;
	
	private GridView gridView;
	
    private RelativeLayout facesLayout;
	
    public boolean isFace = false;
    
    private long id =0;
	 
    private Toast sendToast;
    
	//返回
	public static final int COME_BACK = 1;
	
	//发布评论
	public static final int POST_COMMENT = 2;
	
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		WeiboUtil.closeWindowTitle(this);
		try{
			id = getIntent().getExtras().getLong("statusId");
			if(id==0){
				finish();
			}
		}catch(Exception e){
			Log.e("CommentActivity","onCreate");
			finish();
		}
		
		setContentView(R.layout.comment);
		this.initControlsAndAddControlsEvent();
		MainService.addActivity(this);
		
	}

	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		int i=140-arg0.length();
		tvCmtLabel.setText(String.valueOf(i));
//		tvCmtLabel.setText("可输入字数:"+i);
	}


	public void onClick(View v) {
		 int tag = (Integer) v.getTag();
		 switch (tag) {
			case COME_BACK:
				this.finish();
				break;
				
			case POST_COMMENT:
				 
		         String comment = editText.getText().toString();
		         if(!WeiboUtil.strIsNotEmpty(comment)){
		        	 WeiboUtil.myToastShow(this, "您还是说点什么吧");
		        	 return;
		         }
		         HashMap hm=new HashMap();
				 hm.put("msg", comment);
				 hm.put("id", String.valueOf(id));
				 if(rb_forward.isChecked()){
					 hm.put("news",new Boolean(true));
					 break;
				  }
				 Task ts=new Task(Task.TASK_NEW_WEIBO_COMMENT,hm);
				 MainService.newTaskToTaskQueneHead(ts);
//				 finish();
				 sendToast =  WeiboUtil.myToastShowProceessBar(this, "正在发布,请稍后...");
			     sendToast.show();
			default:
				break;
				
       
		}
	
	}
	



	@Override
	public void init() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void refresh(Object... param) {
		//关闭发送精度条
		sendToast = null;
		WeiboUtil.myToastShow(this, (String) param[1]);
		finish();
	}

	@Override
	public void initControlsAndAddControlsEvent() {
		title = findViewById(R.id.title4Comm);
		titleTv = (TextView)title.findViewById(R.id.textView);
		titleTv.setText(R.string.itemmenu_comment);
//		final ImageView imageViewTitle = (ImageView) title.findViewById(R.id.imageViewTitle);
//    	imageViewTitle.setImageResource(R.drawable.comment);
		
		back = (Button)title.findViewById(R.id.title_bt_left);
//		back.setText(R.string.imageviewer_back);
		
		back.setBackgroundResource(R.drawable.title_back);
		
		back.setTag(COME_BACK);
		back.setOnClickListener(this);
		
//		back.setOnClickListener(new OnClickListener(){
		send = (Button)findViewById(R.id.title_bt_right);
		
		//send.setText(R.string.title_button_send);
		send.setBackgroundResource(R.drawable.title_post);
		
		send.setTag(POST_COMMENT);
		
		send.setOnClickListener(this);
		editText = (EditText)findViewById(R.id.etCmtReason);
		editText.addTextChangedListener(this);
		//输入提示
		tvCmtLabel =(TextView)findViewById(R.id.tvCmtLabel);
//		tvCmtLabel.setText("可输入字数140");
		
		rb_forward = (CheckBox)findViewById(R.id.rb_forward);
		
		 this.btnFaces = (ImageButton) this.findViewById(R.id.ibInsertFace);
		    //添加话题
		 this.ibInsertTopic = (ImageButton) this.findViewById(R.id.ibInsertTopic);
		    //插入用户名
	    this.ibInsertAt = (ImageButton) this.findViewById(R.id.ibInsertAt);
	    //显示表情的GridView
        this.gridView = (GridView)this.findViewById(R.id.gridView);   
        this.gridView.setAdapter(new ImageFaceAdapter(CommentActivity.this));
        
        //网状的事件监听
        this.gridView.setOnItemClickListener(new OnItemClickListener() {
 			public void onItemClick(AdapterView<?> parent, View v, int position, long id){
 				Toast.makeText(CommentActivity.this, "你选择了" + (position + 1) + " 号图片", Toast.LENGTH_SHORT).show();
 			    ////需要处理的文本，[faces]是需要被替代的文本   
 				SpannableString spannableString = new SpannableString(editText.getText().toString() + "[大哥]");
 			    Drawable drawable = CommentActivity.this.getResources().getDrawable(R.drawable.face1 + position);
 			    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
 			    
 			    //添加图片主要用SpannableString和ImageSpan类
 			    
 			    ////要让图片替代指定的文字就要用ImageSpan
 			    ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
 			    //开始替换，注意第2和第3个参数表示从哪里开始替换到哪里替换结束（start和end） 
 			    //最后一个参数类似数学中的集合,[5,12)表示从5到12，包括5但不包括12  
 			    spannableString.setSpan(span, 
 			    		editText.getText().length(),
 			    		editText.getText().length()+"[大哥]".length(), 
 			 		    Spannable.SPAN_INCLUSIVE_EXCLUSIVE
 			 		   );  
 			   editText.setText(spannableString);
// 			    NewWeiboActivity.this.etNewBlog.setC
 			   // Selection.setSelection(etNewBlog, etNewBlog.length()-1); 
 			  editText.setSelection(editText.length()-1, editText.length());
 			}
 		});
        
      //添加表情
		this.btnFaces.setOnClickListener(new OnClickListener(){
	           
				public void onClick(View v) {
					//Toast.makeText(NewWeiboActivity.this, "选择表情", Toast.LENGTH_SHORT).show();
					CommentActivity.this.isFace = !CommentActivity.this.isFace;
					if(CommentActivity.this.facesLayout == null){
						CommentActivity.this.facesLayout = (RelativeLayout) CommentActivity.this.findViewById(R.id.facesLayout);
					}
				 
					if(CommentActivity.this.isFace){
						CommentActivity.this.facesLayout.setVisibility(View.VISIBLE);
						CommentActivity.this.gridView.setVisibility(View.VISIBLE);
					}else{
						CommentActivity.this.facesLayout.setVisibility(View.GONE);
						CommentActivity.this.gridView.setVisibility(View.GONE);
					}
				}
	       }); 
		
      //添加话题
        this.ibInsertTopic.setOnClickListener(new ImageButton.OnClickListener(){

			 @Override
			 public void onClick(View v) {
				 Toast.makeText(CommentActivity.this, "插入话题", Toast.LENGTH_SHORT).show(); 
				String blogContent = editText.getText().toString();
				CommentActivity.this.editText.setText(new StringBuffer(blogContent).append("##").toString());
//				blogContent = editText.getText().toString();
//				SpannableString spannableString = new SpannableString(blogContent);
//	            ForegroundColorSpan span=new ForegroundColorSpan(Color.RED);   
//	            spannableString.setSpan(span, blogContent.length()-9, blogContent.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);   
//	            editText.setText(spannableString);   
			 }
	       });
	

        this.ibInsertAt.setOnClickListener(new ImageButton.OnClickListener(){

 		 @Override
 		 public void onClick(View v) {
 			Toast.makeText(CommentActivity.this, "用户名", Toast.LENGTH_SHORT).show(); 
 			String blogContent = editText.getText().toString();
 			CommentActivity.this.editText.setText(new StringBuffer(blogContent).append("@").toString());
// 			blogContent = editText.getText().toString();
// 			SpannableString spannableString = new SpannableString(blogContent);
//            ForegroundColorSpan span=new ForegroundColorSpan(Color.RED);   
//            spannableString.setSpan(span, blogContent.length()-8, blogContent.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);   
//            editText.setText(spannableString);   
 		 }
        });
		
	}
	
	 
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}


	public void afterTextChanged(Editable arg0) {
	}


	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {
	}
}
